﻿/** 
 * adds open social 0.7 support for gadgets namespace to happy hour
 */
var QueryString = function(opt) {
    var qs = window.location.search.substring(1,window.location.search.length);
    
    if (typeof(opt) != 'undefined' && opt != null) {
        qs = opt.toString();
    }
    
    qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&');
	this.params = {};
	
    for (var i = 0; i < args.length; i++) {
        var value;
        var pair = args[i].split('=');
        var name = unescape(pair[0]);

        if (pair.length == 2) {
           value = unescape(pair[1]);
        }
        else {
            value = name;
        }
        
        this.params[name] = value
    }
}; 

QueryString.prototype.Get = function(key, default_) {
    if (!default_) default_ = null;
    var value = this.params[key];
    if (value==null) value = default_;
    return(value);
};
    
QueryString.prototype.toString = function() {
    return(qs);
};

/**
 * Simulate _IG_RegisterOnloadHandler since many widgets use it
 */
function _IG_RegisterOnloadHandler(callback) {
    window.setTimeout(function() {
        callback();
    }, 100);
};

/**
 * Simulate _IG_AdjustIFrameHeight since many widgets use it
 */
function _IG_AdjustIFrameHeight() {
    // no-op for this container
};

var gadgets = gadgets || {};

if (gadgets.views) {
    // hijack shindig
    gadgets.views.requestNavigateTo = function(view, opt_params) {
        if (parent != null && parent != window) {
            parent._viewParameters = opt_params; 
            parent.gadgets._setView(view.getName());
        }
        else { 
            window._viewParameters = opt_params; 
            gadgets._setView(view.getName());
        }
    };
    
    // hijack shindig
    gadgets.views.ViewType = gadgets.util.makeEnum([
        "CANVAS", "PROFILE"
    ]);
    
    // hijack shindig
    gadgets.views.getCurrentView = function() {
        return(gadgets._currentView);
    };
    
    // hijack shindig
    gadgets.views.getParams = function() {
        return(window._viewParameters);
    };
    
    // hijack shindig
    gadgets.views.getSupportedViews = function() {
        var canvas = new gadgets.views.View(gadgets.views.ViewType.CANVAS, true);
        var profile = new gadgets.views.View(gadgets.views.ViewType.PROFILE, true); 
        
        return({
            'CANVAS':canvas, 
            'PROFILE':profile 
        });
    };
};

if (gadgets.window) {
    gadgets.window.adjustHeight = function() {
        // TODO: add support
    }
}

if (gadgets.io) {
    // hijack shindig
    gadgets.io.makeRequest = function(url, callback, opt_params) {
        var cType = gadgets.io.ContentType.JSON;
        
        if (typeof(opt_params) != 'undefined') {
            if (opt_params[gadgets.io.RequestParameters.CONTENT_TYPE]) {
                cType = opt_params[gadgets.io.RequestParameters.CONTENT_TYPE];
            }
        } 
        
        if (cType == gadgets.io.ContentType.JSON) {
            jQuery.getJSON(url, opt_params, callback);
        } 
        else {
            jQuery.get(url, opt_params, callback);
        }
    };
};

if (!gadgets.json && JSON) {
    gadgets.json = JSON;
}; 

if (gadgets.util) {
    // hijack shindig
    gadgets.util.registerOnLoadHandler = function(callback) {
        _IG_RegisterOnloadHandler(callback);
    }; 
};

if (gadgets) {
    // happy hour gadgets.* extensions
    gadgets._currentView = null, 
    gadgets._debugMode = false, 
    gadgets._cacheBust = false, 
    gadgets._iframeMode = false, 
    gadgets._currentView = null, 
    gadgets._setView = function(viewType) {
        var view = new gadgets.views.View(viewType, true);
        
        var win = window;
        
        if (parent != null && parent != window) {
            win = parent;
        }
        
        win._gadgetLoaded = false;
        
        var qs = new QueryString();
        
        var txtSurface = jQuery('#HappyHour-surface');
        var selSurface = jQuery('#HappyHour-surfaceList');
        
        if (txtSurface) {
            txtSurface.val(viewType);
        }
        
        if (selSurface && selSurface[0]) {             
            if (viewType == gadgets.views.ViewType.CANVAS) {
                selSurface[0].selectedIndex = 1;
            }
            else if (viewType == gadgets.views.ViewType.PROFILE) {
                selSurface[0].selectedIndex = 0;
            }
        }  
        
        if (this._currentView != null && view != this._currentView) {
            if (win.happyHourManager) {
                win.happyHourManager.init(); 
            }
        }

        this._currentView = view; 
    };
};

